package com.yimi.yinhepda.utils

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.*
import com.pda.ym.base.CommonViewHolder
import com.pda.ym.callbacks.UICallBack
import com.pda.ym.entity.bean.CarPlanTaskEntity
import com.pda.ym.entity.bean.LoadBillTaskEntity
import com.pda.ym.utils.Constants
import com.pda.ym.utils.SoundVibratorManager
import com.yimi.yinhepda.R
import com.yimi.yinhepda.adapter.RecycleAdapter.CheckBoxDialogAdapter
import com.yimi.yinhepda.adapter.RecycleAdapter.SelectNextStowageTaskAdapter
import com.yimi.yinhepda.adapter.RecycleAdapter.SelectStowageNoAdapter
import com.yimi.yinhepda.callbacks.OnClickCallBack
import com.yimi.yinhepda.entity.bean.CheckBoxItemEntity
import com.yimi.yinhepda.views.load_module.LoadUtil
import com.yimi.yinhepda.views.load_module.LoadUtil.checkSelectStowageTask
import com.yimi.yinhepda.views.load_module.LoadUtil.createCheckTaskType

/**
 * @author: lj
 * Date: 2020/5/8 16:04
 * Description:对话框工具类
 */
class DialogUtil {

    companion object {
        lateinit var checkBoxAdapter: CheckBoxDialogAdapter

        fun checkBoxDialog(context: Context, title: String, btnSure: String, btnCancle: String, dataList: MutableList<CheckBoxItemEntity>?,
                           clickCallBack: OnClickCallBack) {
            checkBoxDialog(context, title, btnSure, btnCancle, dataList, false, false, null, clickCallBack)
        }

        fun checkBoxDialog(context: Context, title: String, btnSure: String, btnCancle: String, dataList: MutableList<CheckBoxItemEntity>?,
                           isCreateTask: Boolean, planTasks: MutableList<CarPlanTaskEntity>, clickCallBack: OnClickCallBack) {
            checkBoxDialog(context, title, btnSure, btnCancle, dataList, false, isCreateTask, planTasks, clickCallBack)
        }

        fun checkBoxDialog(context: Context, title: String, btnSure: String, btnCancle: String, dataList: MutableList<CheckBoxItemEntity>?,
                           singleChoice: Boolean, isCreateTask: Boolean, planTasks: MutableList<CarPlanTaskEntity>?, clickCallBack: OnClickCallBack) {
            val layout = LayoutInflater.from(context).inflate(R.layout.alert_dialog_check_box, null) as LinearLayout
            val tvTitle = layout.findViewById<TextView>(R.id.tv_title)
            tvTitle.text = title
            val etQuerynext = layout.findViewById<EditText>(R.id.et_query_next)
            val llCbTaskType = layout.findViewById<LinearLayout>(R.id.ll_cb_taskType)
            val cbYm = layout.findViewById<CheckBox>(R.id.cb_ym)
            val cbUC = layout.findViewById<CheckBox>(R.id.cb_uc)
            val rvDataList: RecyclerView = layout.findViewById(R.id.rv_select_item)
            val btnOK = layout.findViewById<Button>(R.id.btn_sure)
            btnOK.text = btnSure
            val btnBack = layout.findViewById<Button>(R.id.btn_back)
            btnBack.text = btnCancle
            val dialog = Dialog(context, R.style.AsyncTaskDialog)
            dialog.addContentView(layout, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT))
            dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
            dialog.window!!.decorView.setPadding(20, 20, 20, 20)
            dialog.setCanceledOnTouchOutside(false)
            dialog.setCancelable(false)
            dialog.show()
            val layoutParams = dialog.window!!.attributes
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            dialog.window!!.attributes = layoutParams

            if (isCreateTask) {
                llCbTaskType.visibility = View.VISIBLE
                //默认选择 快运 或 者快 递类型
                createCheckTaskType(planTasks, cbYm, cbUC)
            } else {
                llCbTaskType.visibility = View.GONE
            }

            val localDataList = mutableListOf<CheckBoxItemEntity>()
            dataList?.let { localDataList.addAll(it) }

            val tempList = mutableListOf<CheckBoxItemEntity>()
            tempList.addAll(localDataList)

            checkBoxAdapter = CheckBoxDialogAdapter(context, localDataList, object : CommonViewHolder.onItemCommonClickListener {
                override fun onItemClickListener(position: Int) {
                    if (position == RecyclerView.NO_POSITION) {
                        return
                    }
                    val entity = localDataList[position]
                    if (singleChoice) {
                        localDataList.forEach {
                            it.isCheck = false
                            it.isTempCheck = false
                        }
                        entity.isTempCheck = true
                    } else {
                        entity.isTempCheck = !entity.isTempCheck
                    }
                    checkBoxAdapter.notifyDataSetChanged()
                }

                override fun onItemLongClickListener(position: Int) {}
            })
            rvDataList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            rvDataList.adapter = checkBoxAdapter

            btnOK.setOnClickListener {
                //返回已选内容
                val list = mutableListOf<CheckBoxItemEntity>()
                localDataList.forEach {
                    it.isCheck = it.isTempCheck
                    if (it.isTempCheck) {
                        if (isCreateTask) {

                            if (!cbYm.isChecked && !cbUC.isChecked) {
                                ToastUtil.showToast("请至少选择一个任务类型！")
                                SoundVibratorManager.playSound(2)
                                return@setOnClickListener
                            }

                            if (cbYm.isChecked && cbUC.isChecked) {
                                it.createType = 0
                            } else if (cbYm.isChecked) {
                                it.createType = 1
                            } else {
                                it.createType = 2
                            }
                        }
                        list.add(it)
                    }
                }
                clickCallBack.onSure(list, dialog)
                dialog.dismiss()
            }

            btnBack.setOnClickListener {
                dialog.dismiss()
                localDataList.forEach {
                    it.isTempCheck = it.isCheck
                }
                clickCallBack.onCancle()
            }

            etQuerynext.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                    if (s.isNotEmpty()) {
                        if (!tempList.isNullOrEmpty()) {
                            val nextDept = s.toString()
                            val lists = mutableListOf<CheckBoxItemEntity>()
                            for (item in tempList) {
                                if (item.name.contains(nextDept) || item.code.contains(nextDept, true)) {
                                    lists.add(item)
                                }
                            }
                            if (lists.isNotEmpty()) {
                                localDataList.clear()
                                localDataList.addAll(lists)
                                checkBoxAdapter.notifyDataSetChanged()
                            } else {
                                if (title == Constants.PLEASE_SELECT_NEXT_DEPT) {
                                    //查询下一站
                                    querySourceArriveByStation(1, s.toString(), localDataList)
                                } else {
                                    //查询上一站
                                    querySourceArriveByStation(2, s.toString(), localDataList)
                                }
                            }
                        } else {
                            if (title == Constants.PLEASE_SELECT_NEXT_DEPT) {
                                //查询下一站
                                querySourceArriveByStation(1, s.toString(), localDataList)
                            } else {
                                //查询上一站
                                querySourceArriveByStation(2, s.toString(), localDataList)
                            }
                        }
                    } else {
                        localDataList.clear()
                        localDataList.addAll(tempList)
                        checkBoxAdapter.notifyDataSetChanged()
                    }
                }

                override fun afterTextChanged(s: Editable) {}
            })
        }

        /**
         * 查询上下一站
         */
        private fun querySourceArriveByStation(operType: Int, s: String, localDataList: MutableList<CheckBoxItemEntity>) {
            LoadUtil.querySourceArriveByStation(operType, s, object : UICallBack() {
                override fun onSuccess(obj: Any?) {
                    val lineList = obj as MutableList<CheckBoxItemEntity>
                    localDataList.clear()
                    localDataList.addAll(lineList)
                    checkBoxAdapter.notifyDataSetChanged()
                }

                override fun onError(msg: String?) {
                    ToastUtil.showToast(msg)
                }
            })
        }

        lateinit var mSelectStowageAdapter: SelectNextStowageTaskAdapter

        /**
         * 选择装车任务对话框
         */
        fun selectTaskDialog(context: Context, dataList: MutableList<LoadBillTaskEntity>?,
                             planTasks: MutableList<CarPlanTaskEntity>?, clickCallBack: OnClickCallBack) {
            val layout = LayoutInflater.from(context).inflate(R.layout.alert_dialog_select_load_task, null) as LinearLayout
            val tvTitle = layout.findViewById<TextView>(R.id.tv_title)
            tvTitle.text = "选择装车任务"
            val rvDataList: RecyclerView = layout.findViewById(R.id.rv_select_item)
            val btnOK = layout.findViewById<Button>(R.id.btn_sure)
            btnOK.text = "装车"
            val btnBack = layout.findViewById<Button>(R.id.btn_back)
            btnBack.text = "取消"
            val dialog = Dialog(context, R.style.AsyncTaskDialog)
            dialog.addContentView(layout, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT))
            dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
            dialog.window!!.decorView.setPadding(20, 20, 20, 20)
            dialog.setCanceledOnTouchOutside(false)
            dialog.setCancelable(false)
            dialog.show()
            val layoutParams = dialog.window!!.attributes
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            dialog.window!!.attributes = layoutParams

            mSelectStowageAdapter = SelectNextStowageTaskAdapter(context, dataList, object : CommonViewHolder.onItemCommonClickListener {

                override fun onItemClickListener(position: Int) {
                    if (position == RecyclerView.NO_POSITION) {
                        return
                    }
                    val entity: LoadBillTaskEntity = dataList!![position]
                    entity.isCheck = !entity.isCheck
                    mSelectStowageAdapter.notifyDataSetChanged()
                }

                override fun onItemLongClickListener(position: Int) {
                }

            })
            rvDataList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            rvDataList.adapter = mSelectStowageAdapter

            btnOK.setOnClickListener onclick@{

                if (!checkSelectStowageTask(dataList, planTasks)) {
                    return@onclick
                }

                dialog.dismiss()
                clickCallBack.onSure(null, null)
            }

            btnBack.setOnClickListener {
                dialog.dismiss()
            }

        }

        var mStowageNoAdapter:SelectStowageNoAdapter?=null
        fun showUnOrLoadTask(context: Context, dataList: MutableList<LoadBillTaskEntity>?,loadType:Int,clickCallBack: OnClickCallBack){
            val layout = LayoutInflater.from(context).inflate(R.layout.alert_dialog_select_load_task, null) as LinearLayout
            val tvTitle = layout.findViewById<TextView>(R.id.tv_title)
            tvTitle.text = "选择装车任务"
            val rvDataList: RecyclerView = layout.findViewById(R.id.rv_select_item)
            val btnOK = layout.findViewById<Button>(R.id.btn_sure)
            btnOK.text = "装车"
            val btnBack = layout.findViewById<Button>(R.id.btn_back)
            btnBack.text = "取消"
            btnBack.visibility=View.GONE
            btnOK.visibility=View.GONE
            val dialog = Dialog(context, R.style.AsyncTaskDialog)
            dialog.addContentView(layout, LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT))
            dialog.window!!.setBackgroundDrawableResource(android.R.color.transparent)
            dialog.window!!.decorView.setPadding(20, 20, 20, 20)
            dialog.setCanceledOnTouchOutside(false)
            dialog.setCancelable(false)
            dialog.show()
            val layoutParams = dialog.window!!.attributes
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
            dialog.window!!.attributes = layoutParams

            mStowageNoAdapter= SelectStowageNoAdapter(loadType,context, dataList,object:CommonViewHolder.onItemCommonClickListener{
             override fun onItemLongClickListener(position: Int) {
             }

             override fun onItemClickListener(position: Int) {
                 ListUtils.judgeCheck(position,dataList)
                 mStowageNoAdapter!!.notifyDataSetChanged()
                 dialog.dismiss()
                 if (clickCallBack!=null){
                     clickCallBack.onSuccess(dataList!!.get(position))
                 }
             }
         })
        rvDataList.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        rvDataList.adapter = mStowageNoAdapter
        }

    }



}